/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Frontera;

import Entidad.Cliente;
import control.ControlCliente;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Juan
 */
public class FBuscarClientes extends javax.swing.JFrame {

    /**
     * Creates new form FConsultaConsolidados
     */
    public FBuscarClientes(MenuPrincipalAlterno padre) {
        initComponents();
        this.padre = padre;
        inicializar();
        this.setLocationRelativeTo(null);
    }
    
    private void inicializar() {
        SFechaInicial.setEditor(new JSpinner.DateEditor(SFechaInicial, "dd/MMMMM/yyyy"));
        SFechaFinal.setEditor(new JSpinner.DateEditor(SFechaFinal, "dd/MMMMM/yyyy"));
        
        tca = new TableColumnAdjuster(TResultados);
        tca.adjustColumns();
        TResultados.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        TResultados.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (TResultados.getSelectedRow() == -1)
                    BMasInfo.setEnabled(false);
                else
                    BMasInfo.setEnabled(true);
            }
        });
        
        ActionListener listener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JCheckBox source = (JCheckBox) e.getSource();
                source.setSelected(source.isSelected());
                CBConsultarTodos.setSelected(false);
            }
        };
        
        checkboxes.add(CBNombre);
        checkboxes.add(CBAbreviatura);
        checkboxes.add(CBCiudad);
        checkboxes.add(CkBCondicionesFacturacion);
        checkboxes.add(CBFecha);
        checkboxes.add(CBIdentificacion);
        checkboxes.add(CkBTipoIdentificacion);
        
        for (JCheckBox checkBox : checkboxes) {
            checkBox.addActionListener(listener);
        }
    }
    
    private boolean algunoSeleccionado(){
        boolean algunoSeleccionado = false;
        for (JCheckBox checkBox : checkboxes) {
            if (checkBox.isSelected()){
                algunoSeleccionado = true;
                break;
            }
        }
        return algunoSeleccionado;
    }
    
    private void colocarClientesEnTabla(){
        if (listaClientes != null && listaClientes.size() > 0) {
            limpiarTabla();
            DefaultTableModel model = (DefaultTableModel) TResultados.getModel();
            for (Cliente cliente : listaClientes) {
                model.addRow(new Object[] {cliente.getNombre(),
                                           cliente.getAbreviatura(),
                                           cliente.getTipoIdentificacion()+" "+cliente.getIdentificacion(),
                                           cliente.getTelefonos(),
                                           cliente.getDireccion(),
                                           cliente.getCiudad(),
                                           cliente.getCondicionesFacturacion(),
                                           cliente.getFechaRegistro()});
            }
            tca.adjustColumns();
            LResultados.setText(String.format("%d resultado(s)", listaClientes.size()));
        } else {
            limpiarTabla();
            LResultados.setText("No se encontraron resultados.");
        }
    }
    
    private void limpiarTabla(){
        TResultados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre", "Abreviatura", "Identificación", "Teléfonos", "Dirección", "Ciudad", "Condiciones de Facturación", "Fecha de Registro"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            @Override
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
    }
    
    public Cliente getClienteActual() {
        int filaSeleccionada = TResultados.getSelectedRow();
        if (filaSeleccionada != -1)
            return listaClientes.get(filaSeleccionada);
        else
            return null;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        TFNombre = new javax.swing.JTextField();
        TFAbreviatura = new javax.swing.JTextField();
        TFIdentificacion = new javax.swing.JTextField();
        TFOtro = new javax.swing.JTextField();
        TFCiudad = new javax.swing.JTextField();
        CBTipoIdentificacion = new javax.swing.JComboBox();
        CBCondicionesFacturacion = new javax.swing.JComboBox();
        SFechaInicial = new javax.swing.JSpinner();
        SFechaFinal = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        CBNombre = new CheckBoxExtendido(TFNombre);
        CBIdentificacion = new CheckBoxExtendido(TFIdentificacion);
        CBAbreviatura = new CheckBoxExtendido(TFAbreviatura);
        CkBTipoIdentificacion = new CheckBoxExtendido(CBTipoIdentificacion);
        CBCiudad = new CheckBoxExtendido(TFCiudad);
        CkBCondicionesFacturacion = new CheckBoxExtendido(CBCondicionesFacturacion);
        CBFecha = new CheckBoxExtendido(SFechaInicial);
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        BBuscar = new javax.swing.JButton();
        BCancelar = new javax.swing.JButton();
        CBConsultarTodos = new javax.swing.JCheckBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        TResultados = new javax.swing.JTable();
        BMasInfo = new javax.swing.JButton();
        LResultados = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Consultar Clientes");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Consultar Clientes"));

        TFNombre.setEnabled(false);

        TFAbreviatura.setEnabled(false);

        TFIdentificacion.setEnabled(false);

        TFOtro.setEnabled(false);

        TFCiudad.setEnabled(false);

        CBTipoIdentificacion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cédula", "NIT", "Otro" }));
        CBTipoIdentificacion.setEnabled(false);
        CBTipoIdentificacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBTipoIdentificacionActionPerformed(evt);
            }
        });
        CBTipoIdentificacion.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                CBTipoIdentificacionPropertyChange(evt);
            }
        });

        CBCondicionesFacturacion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Al recibir la Muestra", "Fechas/Horarios de Radicación", "Lugar de Radicación", "Forma de Envío" }));
        CBCondicionesFacturacion.setEnabled(false);

        SFechaInicial.setModel(new javax.swing.SpinnerDateModel());
        SFechaInicial.setEnabled(false);
        SFechaInicial.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                SFechaInicialPropertyChange(evt);
            }
        });

        SFechaFinal.setModel(new javax.swing.SpinnerDateModel());
        SFechaFinal.setEnabled(false);

        jLabel2.setText("Filtar por:");

        CBNombre.setText("Nombre");

        CBIdentificacion.setText("Identificación");

        CBAbreviatura.setText("Abreviatura");

        CkBTipoIdentificacion.setText("Tipo");

        CBCiudad.setText("Ciudad");

        CkBCondicionesFacturacion.setText("Condiciones de Facturación");

        CBFecha.setText("Fecha de registro");

        jLabel3.setText("Inicial:");

        jLabel4.setText("Final:");

        BBuscar.setText("Buscar");
        BBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BBuscarActionPerformed(evt);
            }
        });

        BCancelar.setText("Cancelar");
        BCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCancelarActionPerformed(evt);
            }
        });

        CBConsultarTodos.setText("Consultar Todos");
        CBConsultarTodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBConsultarTodosActionPerformed(evt);
            }
        });

        TResultados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre", "Abreviatura", "Identificación", "Teléfonos", "Dirección", "Ciudad", "Condiciones de Facturación", "Fecha de Registro"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        TResultados.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jScrollPane1.setViewportView(TResultados);

        BMasInfo.setText("Más Información");
        BMasInfo.setEnabled(false);
        BMasInfo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BMasInfoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(CBIdentificacion)
                                                    .addComponent(CBAbreviatura)
                                                    .addComponent(CBNombre))
                                                .addGap(74, 74, 74))
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(CkBTipoIdentificacion)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(CBTipoIdentificacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18, 18)))
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(TFAbreviatura, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                                            .addComponent(TFIdentificacion, javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(TFOtro)
                                            .addComponent(TFNombre))
                                        .addGap(35, 35, 35)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(CkBCondicionesFacturacion)
                                                    .addComponent(CBCiudad))
                                                .addGap(18, 18, 18)
                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                    .addComponent(CBCondicionesFacturacion, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addComponent(TFCiudad)))
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                                        .addComponent(CBFecha)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                        .addComponent(jLabel3))
                                                    .addComponent(CBConsultarTodos))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(SFechaInicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18, 18)
                                                .addComponent(jLabel4)
                                                .addGap(18, 18, 18)
                                                .addComponent(SFechaFinal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                                .addGap(0, 16, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(BBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(BCancelar)
                                .addGap(18, 18, 18)
                                .addComponent(LResultados))
                            .addComponent(BMasInfo))
                        .addGap(0, 541, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CBNombre)
                    .addComponent(TFNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CBCiudad)
                    .addComponent(TFCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CBAbreviatura)
                    .addComponent(TFAbreviatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CkBCondicionesFacturacion)
                    .addComponent(CBCondicionesFacturacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CBIdentificacion)
                    .addComponent(TFIdentificacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CBFecha)
                    .addComponent(jLabel3)
                    .addComponent(SFechaInicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(SFechaFinal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CkBTipoIdentificacion)
                    .addComponent(CBTipoIdentificacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TFOtro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CBConsultarTodos))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(BBuscar)
                        .addComponent(BCancelar))
                    .addComponent(LResultados, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(BMasInfo)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_BCancelarActionPerformed

    private void BBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BBuscarActionPerformed
        ControlCliente controlCliente = new ControlCliente();
        LResultados.setText("Buscando...");
        
        if (CBConsultarTodos.isSelected())
            try {
                listaClientes = controlCliente.cargarTodos();
                colocarClientesEnTabla();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, e.getMessage(), "Error SQL", JOptionPane.ERROR_MESSAGE);
                this.dispose();
            }
        else
            if (algunoSeleccionado()) {
                Cliente cliente = new Cliente();
                
                if (CBNombre.isSelected())
                    cliente.setNombre(TFNombre.getText().trim());
                
                if (CBAbreviatura.isSelected()) {
                    String abreviatura = TFAbreviatura.getText().trim();
                    if (abreviatura != null && abreviatura.length() <= 3)
                        cliente.setAbreviatura(abreviatura);
                    else {
                        JOptionPane.showMessageDialog(this, "El campo abreviatura puede contener máximo tres caracteres.",
                                "Información", JOptionPane.INFORMATION_MESSAGE);
                        return;
                    }  
                }
                
                if (CBIdentificacion.isSelected())
                    cliente.setIdentificacion(TFIdentificacion.getText().trim());
                
                if (CkBTipoIdentificacion.isSelected())
                    if (CBTipoIdentificacion.getSelectedIndex() == 0)
                        cliente.setTipoIdentificacion("Cedula");
                    else if (CBTipoIdentificacion.getSelectedIndex() == 1)
                        cliente.setTipoIdentificacion("NIT");
                    else
                        cliente.setTipoIdentificacion(TFOtro.getText().trim());
                
                if (CBCiudad.isSelected())
                    cliente.setCiudad(TFCiudad.getText().trim());
                
                if (CkBCondicionesFacturacion.isSelected())
                    cliente.setCondicionesFacturacion((String) CBCondicionesFacturacion.getSelectedItem());
                
                if (CBFecha.isSelected()) {
                    SpinnerDateModel modeloFechaInicial = (SpinnerDateModel) SFechaInicial.getModel();
                    SpinnerDateModel modeloFechaFinal = (SpinnerDateModel) SFechaFinal.getModel();
                    java.sql.Date fechaInicial = new java.sql.Date(modeloFechaInicial.getDate().getTime());
                    java.sql.Date fechaFinal = new java.sql.Date(modeloFechaFinal.getDate().getTime());
                    try {
                        listaClientes = controlCliente.buscarClientes(cliente, fechaInicial, fechaFinal);
                    } catch (Exception e) {
                        JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                        this.dispose();
                    }
                } else {
                    try {
                        listaClientes = controlCliente.buscarClientes(cliente, null, null);
                    } catch (Exception e) {
                        JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                        this.dispose();
                    }
                }
                
                colocarClientesEnTabla();
                
            } else {
                JOptionPane.showMessageDialog(this, "Debe seleccionarse al menos un criterio para buscar.",
                        "Información", JOptionPane.INFORMATION_MESSAGE);
                LResultados.setText(null);
            }
    }//GEN-LAST:event_BBuscarActionPerformed

    private void CBConsultarTodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBConsultarTodosActionPerformed
        for (JCheckBox jCheckBox : checkboxes) {
            jCheckBox.setSelected(false);
        }
    }//GEN-LAST:event_CBConsultarTodosActionPerformed

    private void CBTipoIdentificacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBTipoIdentificacionActionPerformed
        if (CBTipoIdentificacion.getSelectedIndex() == 2)
            TFOtro.setEnabled(true);
        else
            TFOtro.setEnabled(false);
    }//GEN-LAST:event_CBTipoIdentificacionActionPerformed

    private void SFechaInicialPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_SFechaInicialPropertyChange
        if (evt.getPropertyName().equals("enabled"))
            SFechaFinal.setEnabled((Boolean) evt.getNewValue());
    }//GEN-LAST:event_SFechaInicialPropertyChange

    private void CBTipoIdentificacionPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_CBTipoIdentificacionPropertyChange
        if (evt.getPropertyName().equals("enabled"))
            TFOtro.setEnabled((Boolean) evt.getNewValue() && CBTipoIdentificacion.getSelectedIndex() == 2);
    }//GEN-LAST:event_CBTipoIdentificacionPropertyChange

    private void BMasInfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BMasInfoActionPerformed
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                FRegistroClientes fRegistroClientes = new FRegistroClientes();
                fRegistroClientes.setPadre(FBuscarClientes.this);
                fRegistroClientes.inicializarParaConsulta(FBuscarClientes.this.getClienteActual());
                FBuscarClientes.this.setEnabled(false);
                fRegistroClientes.setVisible(true);
            }
        });
    }//GEN-LAST:event_BMasInfoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look anBCancelar For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FBuscarClientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FBuscarClientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FBuscarClientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FBuscarClientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new FBuscarClientes(null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BBuscar;
    private javax.swing.JButton BCancelar;
    private javax.swing.JButton BMasInfo;
    private javax.swing.JCheckBox CBAbreviatura;
    private javax.swing.JCheckBox CBCiudad;
    private javax.swing.JComboBox CBCondicionesFacturacion;
    private javax.swing.JCheckBox CBConsultarTodos;
    private javax.swing.JCheckBox CBFecha;
    private javax.swing.JCheckBox CBIdentificacion;
    private javax.swing.JCheckBox CBNombre;
    private javax.swing.JComboBox CBTipoIdentificacion;
    private javax.swing.JCheckBox CkBCondicionesFacturacion;
    private javax.swing.JCheckBox CkBTipoIdentificacion;
    private javax.swing.JLabel LResultados;
    private javax.swing.JSpinner SFechaFinal;
    private javax.swing.JSpinner SFechaInicial;
    private javax.swing.JTextField TFAbreviatura;
    private javax.swing.JTextField TFCiudad;
    private javax.swing.JTextField TFIdentificacion;
    private javax.swing.JTextField TFNombre;
    private javax.swing.JTextField TFOtro;
    private javax.swing.JTable TResultados;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    private MenuPrincipalAlterno padre;
    private TableColumnAdjuster tca;
    private ArrayList<JCheckBox> checkboxes = new ArrayList<>();
    private ArrayList<Cliente> listaClientes;
}

class CheckBoxExtendido extends JCheckBox {
    private JComponent componente;

    public CheckBoxExtendido(JComponent componente) {
        this.componente = componente;
    }

    @Override
    public void setSelected(boolean b){
        super.setSelected(b);
        componente.setEnabled(b);
    }
}
